<template>
  <div>
    <el-row :gutter="24">
      <el-col :span="4">
        <!-- <div style="padding:20px"> -->

        <el-card class="box-card">
          <el-tree
            ref="treeBox"
            :data="catgoretyTree"
            :props="defaultProps"
            :expand-on-click-node="false"
            :highlight-current="true"
            node-key="departmentId"
            @node-click="handleNodeClick"
          />
        </el-card>
      <!-- </div> -->
      </el-col>
      <el-col :span="20">
        <!--卡片-->
        <el-card class="box-card">
          <!-- 搜索与添加区域 -->
          <el-button v-hasPermi="['admin','product:attrgroup:add']" type="primary" small class="addBtn" style="margin:10px" size="mini" icon="el-icon-plus" @click="addDialogVisible = true">新增
          </el-button>
          <!--表格-->
          <el-table :data="listForm" stripe border style="width: 100%">
            <el-table-column prop="attrGroupId" header-align="center" align="center" label="分组id" width="80" />
            <el-table-column prop="attrGroupName" header-align="center" align="center" label="组名" />
            <el-table-column prop="sort" header-align="center" align="center" label="排序" width="80" />
            <el-table-column prop="descript" header-align="center" align="center" label="描述" />
            <el-table-column prop="icon" header-align="center" align="center" label="组图标" />
            <el-table-column prop="catelogId" header-align="center" align="center" label="所属分类id" />
            <!--操作-->
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button v-hasPermi="['admin','product:attrgroup:relevance']" type="text" size="mini" @click="relevanceBtn(scope.row.attrGroupId)">关联分类</el-button>
                <el-button v-hasPermi="['admin','product:attrgroup:edit']" type="text" size="mini" @click="editBtn(scope.row.attrGroupId)">编辑</el-button>
                <el-button v-hasPermi="['admin','product:attrgroup:del']" type="text" size="mini" @click="delBtn(scope.row.attrGroupId)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <!--分页区域-->
          <el-pagination
            background
            :current-page="pageNumber"
            :page-sizes="[1, 2, 5, 10]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-card>
      </el-col>
    </el-row>
    <!--添加对话框-->
    <el-dialog title="添加" :close-on-click-modal="false" :visible.sync="addDialogVisible" width="580px" @close="addHandleClose">
      <span>
        <el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="100px">
          <el-form-item label="组名" prop="attrGroupName">
            <el-input v-model="addForm.attrGroupName" />
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input-number v-model.number="addForm.sort" :min="1" :max="999" controls-position="right" style="width: 178px;" />
          </el-form-item>
          <el-form-item label="描述" prop="descript">
            <el-input v-model="addForm.descript" />
          </el-form-item>
          <el-form-item label="组图标" prop="icon">
            <el-input v-model="addForm.icon" />
          </el-form-item>
          <el-form-item label="所属分类id" prop="catelogIds">
            <el-cascader
              v-model="addForm.catelogIds"
              placeholder="试试搜索：手机"
              filterable
              :options="catgoretyTree"
              :props="defaultProps"
              style="width: 450px"
            />
          </el-form-item>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addClick">确 定</el-button>
      </span>
    </el-dialog>

    <!--编辑对话框-->
    <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="editDialogVisible" width="580px" @close="editHandleClose">
      <span>
        <el-form ref="editFormRef" :model="editForm" label-width="100px">
          <el-form-item label="组名" prop="attrGroupName">
            <el-input v-model="editForm.attrGroupName" />
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input-number v-model.number="editForm.sort" :min="1" :max="999" controls-position="right" style="width: 178px;" />
          </el-form-item>
          <el-form-item label="描述" prop="descript">
            <el-input v-model="editForm.descript" />
          </el-form-item>
          <el-form-item label="组图标" prop="icon">
            <el-input v-model="editForm.icon" />
          </el-form-item>
          <el-form-item label="所属分类id" prop="catelogIds">
            <el-cascader
              v-model="editForm.catelogIds"
              placeholder="试试搜索：手机"
              filterable
              :options="catgoretyTree"
              :props="defaultProps"
              style="width: 450px"
            />
          </el-form-item>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editClick">确 定</el-button>
      </span>
    </el-dialog>

    <!--关联对话框-->
    <el-dialog title="关联" :close-on-click-modal="false" :visible.sync="relevanceDialogVisible" width="580px">
      <span>
        <el-button type="primary" small style="margin:10px" size="mini" icon="el-icon-plus" @click="addrelevanceBtn()">新建关联
        </el-button>
        <!--表格-->
        <el-table :data="relevanceForm" stripe border style="width: 100%">
          <el-table-column prop="attrId" header-align="center" align="center" label="id" width="60px" />
          <el-table-column prop="attrName" header-align="center" align="center" label="属性名" width="120px" />
          <el-table-column prop="valueSelect" header-align="center" align="center" label="可选值" />
          <!--操作-->
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" size="mini" @click="delcategorybrandrelationBtn(scope.row.attrId)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="relevanceDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!--没关联的对话框-->
    <el-dialog title="关联" :close-on-click-modal="false" :visible.sync="norelevanceDialogVisible" width="580px">
      <span>
        <!--表格-->
        <el-table ref="multipleTable" :data="noRelevanceForm" stripe border style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" />
          <el-table-column prop="attrId" header-align="center" align="center" label="id" width="60px" />
          <el-table-column prop="attrName" header-align="center" align="center" label="属性名" width="120px" />
          <el-table-column prop="valueSelect" header-align="center" align="center" label="可选值" />
        </el-table>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addrelevanceClick">添 加</el-button>
      </span>
      <!--分页区域-->
      <el-pagination
        background
        :current-page="relevancePageNumber"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="relevancePageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="relevanceTotal"
        @size-change="relevanceHandleSizeChange"
        @current-change="relevanceHandleCurrentChange"
      />
    </el-dialog>
  </div>
</template>

<script>
import { tree } from '@/api/product/category'
import { add, edit, del, delRelevance, getAttrGroup, listAttrGroup, listAttrGroupAllBygroupId, listAttrGroupAllNoRelevance, addRelation } from '@/api/product/attrgroup'
export default {
  name: 'Attrgroup',
  data() {
    return {
      norelevanceDialogVisible: false,
      relevanceDialogVisible: false,
      addDialogVisible: false,
      editDialogVisible: false,
      pageNumber: 1,
      pageSize: 10,
      total: 0,
      currentCatelogId: 0,
      currentGroupId: 0,
      catgoretyTree: [],
      defaultProps: {
        children: 'children',
        label: 'name',
        value: 'catId'
      },
      attrIds: [],
      noRelevanceForm: [],
      relevancePageNumber: 1,
      relevancePageSize: 10,
      relevanceTotal: 0,
      relevanceForm: [],
      listForm: [],
      addForm: {
        attrGroupId: '',
        attrGroupName: '',
        sort: '',
        descript: '',
        icon: '',
        catelogIds: [],
        catelogId: 0
      },
      editForm: {},
      addFormRules: {
        attrGroupName: [
          { required: true, message: '请输入组名', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入排序', trigger: 'blur' }
        ],
        descript: [
          { required: true, message: '请输入描述', trigger: 'blur' }
        ],
        icon: [
          { required: true, message: '请输入组图标', trigger: 'blur' }
        ],
        catelogId: [
          { required: true, message: '请输入所属分类id', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.initListCatgorety()
  },

  methods: {

    // 查询关联的属性
    relevanceBtn(groupId) {
      this.currentGroupId = groupId
      this.relevanceDialogVisible = true
      // 查询已关联的属性
      this.initListAttrRelevance()
    },
    // 添加新的关联，查询未关联的属性
    addrelevanceBtn() {
      this.norelevanceDialogVisible = true
      this.initListNoAttrRelevance()
    },
    // 未关联的属性全选事件
    handleSelectionChange(val) {
      const arr = new Array(0)
      for (const key in val) {
        arr.push(val[key].attrId)
      }
      this.attrIds = arr
    },
    // 添加未关联的属性
    addrelevanceClick() {
      this.norelevanceDialogVisible = false
      const params = {
        attrIds: this.attrIds,
        attrGroupId: this.currentGroupId
      }
      addRelation(params).then(res => {
        if (res.code === 20000) {
          this.$message.success(res.message)
          // 刷新数据
          this.initListAttrRelevance()
        }
      })
    },

    // 删除关联按钮
    delcategorybrandrelationBtn(attrId) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认操作
        delRelevance(attrId, this.currentGroupId).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListAttrRelevance()
          }
        })
      })
    },

    // 分类点击事件
    handleNodeClick(data) {
      this.currentCatelogId = data.catId
      this.listForm = []
      if (data.catLevel === 3) {
        this.initListAttrGroup()
      }
    },
    // 初始化分类数据
    initListCatgorety() {
      tree().then(res => {
        this.catgoretyTree = res.data
      })
    },

    // 添加按钮
    addBtn() {
      this.addDialogVisible = true
    },

    // 确认添加
    addClick() {
      this.$refs.addFormRef.validate(valid => {
        if (!valid) return
        this.addForm.catelogId = this.addForm.catelogIds[this.addForm.catelogIds.length - 1]
        add(this.addForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListAttrGroup()
          }
          // 关闭对话框
          this.addDialogVisible = false
        })
      })
    },

    // 编辑按钮
    editBtn(id) {
      this.editDialogVisible = true
      getAttrGroup(id).then(res => {
        if (res.code === 20000) {
          this.editForm = res.data
        }
      })
    },

    // 确认编辑
    editClick() {
      this.$refs.editFormRef.validate(valid => {
        if (!valid) return
        edit(this.editForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListAttrGroup()
          }
          // 关闭对话框
          this.editDialogVisible = false
        })
      })
    },

    // 删除按钮
    delBtn(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认操作
        del(id).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListAttrGroup()
          }
        })
      })
    },

    // 监听当前页的事件
    handleCurrentChange(pageIndex) {
      this.pageNumber = pageIndex
      this.initListAttrGroup()
    },

    // 监听当前页的条数事件
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.initListAttrGroup()
    },

    // 监听当前未关联的属性的分页的事件
    relevanceHandleCurrentChange(pageIndex) {
      this.relevancePageNumber = pageIndex
      this.initListNoAttrRelevance()
    },

    // 监听当前未关联的属性的条目的事件
    relevanceHandleSizeChange(pageSize) {
      this.relevancePageSize = pageSize
      this.initListNoAttrRelevance()
    },

    // 监听添加对话框的关闭事件，重置表单内容
    addHandleClose() {
      this.$refs.addFormRef.resetFields()
      this.addForm.catelogIds = []
    },

    // 监听编辑对话框的关闭事件,重置表单内容
    editHandleClose() {
      this.$refs.editFormRef.resetFields()
      this.editForm.catelogIds = []
    },

    // 查询分类下的组信息
    initListAttrGroup() {
      const params = {
        pageNum: this.pageNumber,
        pageSize: this.pageSize,
        catelogId: this.currentCatelogId
      }
      listAttrGroup(params).then(res => {
        this.listForm = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
      })
    },
    // 查询已关联的属性
    initListAttrRelevance() {
      listAttrGroupAllBygroupId(this.currentGroupId).then(res => {
        this.relevanceForm = res.data
      })
    },
    // 查询未关联的属性
    initListNoAttrRelevance() {
      const params = {
        pageNum: this.relevancePageNumber,
        pageSize: this.relevancePageSize,
        groupId: this.currentGroupId
      }
      listAttrGroupAllNoRelevance(params).then(res => {
        this.noRelevanceForm = res.data.records
        this.relevanceTotal = res.data.total
        this.relevancePageNumber = res.data.current
      })
    }

  }
}
</script>

<style scoped>
</style>
